<template>
  <div class="comments-list">
    <div v-for="comment in comments" :key="comment.id" class="comment-item">
      <img v-img-error :src="comment.userAvatar" alt="用户头像" class="comment-user-avatar">
      <div class="comment-content-wrapper">
        <div class="comment-user-info">
          <span class="comment-username">{{ comment.username }}</span>
          <span class="comment-time">{{ comment.time }}</span>
        </div>
        <div class="comment-content">{{ comment.content }}</div>
        <div class="comment-actions">
          <button class="comment-action-btn" @click="$emit('like-comment', comment.id)">
            <i class="icon-comment-like"></i>
            <span>{{ formatNumber(comment.likes) }}</span>
          </button>
          <button class="comment-action-btn" @click="$emit('reply-comment', comment)">
            <i class="icon-comment-reply"></i>
            <span>回复</span>
          </button>
        </div>
        <!-- 评论回复 -->
        <div v-if="comment.replies && comment.replies.length > 0" class="comment-replies">
          <div v-for="reply in visibleReplies(comment)" :key="reply.id" class="reply-item">
            <span class="reply-username">{{ reply.username }}：</span>
            <span class="reply-content">{{ reply.content }}</span>
          </div>
          <button 
            v-if="comment.replies.length > maxVisibleReplies" 
            class="show-more-replies" 
            @click="toggleShowAllReplies(comment.id)"
          >
            查看全部 {{ comment.replies.length }} 条回复
          </button>
        </div>
      </div>
    </div>
    
    <!-- 加载更多按钮 -->
    <div v-if="hasMore" class="load-more-container">
      <button 
        class="load-more-comments" 
        :disabled="isLoading"
        @click="$emit('load-more-comments')"
      >
        {{ isLoading ? '加载中...' : '加载更多评论' }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CommentList',
  props: {
    comments: {
      type: Array,
      default: () => [],
    },
    isLoading: {
      type: Boolean,
      default: false,
    },
    hasMore: {
      type: Boolean,
      default: true,
    },
    maxVisibleReplies: {
      type: Number,
      default: 2,
    },
  },
  data() {
    return {
      expandedComments: new Set(), // 存储已展开全部回复的评论ID
    }
  },
  methods: {
    formatNumber(num) {
      if (num >= 10000) {
        return (num / 10000).toFixed(1) + '万'
      }
      return num.toString()
    },
    visibleReplies(comment) {
      if (this.expandedComments.has(comment.id)) {
        return comment.replies
      }
      return comment.replies.slice(0, this.maxVisibleReplies)
    },
    toggleShowAllReplies(commentId) {
      if (this.expandedComments.has(commentId)) {
        this.expandedComments.delete(commentId)
      } else {
        this.expandedComments.add(commentId)
      }
    },
  },
}
</script>

<style scoped>
.comments-list {
  margin-top: 20px;
}

.comment-item {
  display: flex;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.comment-user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  flex-shrink: 0;
}

.comment-content-wrapper {
  flex: 1;
}

.comment-user-info {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.comment-username {
  font-weight: 600;
  margin-right: 10px;
  color: #333;
}

.comment-time {
  font-size: 12px;
  color: #999;
}

.comment-content {
  color: #333;
  line-height: 1.6;
  margin-bottom: 10px;
  word-break: break-word;
}

.comment-actions {
  display: flex;
  gap: 20px;
}

.comment-action-btn {
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: color 0.3s ease;
}

.comment-action-btn:hover {
  color: var(--bilibili-pink, #FB7299);
}

.comment-replies {
  margin-top: 12px;
  padding-left: 10px;
  border-left: 2px solid #f0f0f0;
}

.reply-item {
  margin-bottom: 8px;
  font-size: 14px;
  line-height: 1.5;
}

.reply-username {
  font-weight: 600;
  color: #333;
}

.reply-content {
  color: #666;
}

.show-more-replies {
  background: none;
  border: none;
  color: #00a1d6;
  cursor: pointer;
  font-size: 14px;
  margin-top: 5px;
  padding: 0;
  transition: color 0.3s ease;
}

.show-more-replies:hover {
  color: #0086c3;
}

.load-more-container {
  margin-top: 20px;
  text-align: center;
}

.load-more-comments {
  padding: 8px 24px;
  background-color: #f0f2f5;
  border: none;
  border-radius: 4px;
  color: #333;
  cursor: pointer;
  transition: all 0.3s ease;
}

.load-more-comments:hover:not(:disabled) {
  background-color: #e6e8eb;
  transform: translateY(-1px);
}

.load-more-comments:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .comment-item {
    margin-bottom: 15px;
    padding-bottom: 15px;
  }
  
  .comment-user-avatar {
    width: 36px;
    height: 36px;
  }
  
  .comment-actions {
    gap: 15px;
  }
}
</style>